﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Markup;
using Rhizome.Contract;
using Rhizome.Contract.Diag;
using Rhizome.Internal;
using Rhizome.Model.Validation;

namespace Rhizome.Model
{
	[ContentProperty("Transitions")]
	public sealed class Edge : RhizomeElement, IIdentityAccessor<object>, IEventsDiag
	{
		public Edge(object state) : this()
		{
			if (state == null) throw new ArgumentNullException("state");
			State = state;
		}

		public Edge()
		{
			Transitions = new RhizomeElementKeyedCollection<ITransition>(this);
		}

		/// <summary>
		/// 	State ID
		/// </summary>
		public object State { get; set; }

		public KeyedCollection<object, ITransition> Transitions { get; private set; }

		#region Overrides of RhizomeElement

		protected override IEnumerable<IEnumerable> GetElementCollections()
		{
			return base.GetElementCollections().Concat(new[] {Transitions});
		}

		#endregion

		#region IIdentityAccessor<object> Members

		object IIdentityAccessor<object>.Id
		{
			[DebuggerStepThrough]
			get { return State; }
		}

		#endregion

		public override string ToString()
		{
			return string.Format("State: {0}", State);
		}

		#region Implementation of IEventsDiag

		public IEnumerable<object> Events
		{
			get { return from t in Transitions select t.Event; }
		}

		#endregion
	}
}